package com.coocaa.x.app.hall.topic;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.coocaa.x.app.hall.R;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.uipackage.widget.SlideFocusView;

public class HSVLayout extends LinearLayout {
    private HSVAdapter adapter;
    private Context context;
    protected SlideFocusView.FocusViewRevision mFocusViewRevision;
    private SlideFocusView.FocusChangedEvent mFocusChangedEvent = null;
    private OnClickListener mOnClickListener;
    private OnFocusChangeListener mOnFocusChangeListener;
    private OnKeyListener mOnKeyListener;
    private List<View> childViewList;

    public HSVLayout(Context context) {
        super(context);
        this.context = context;
        mFocusViewRevision = new SlideFocusView.FocusViewRevision(CoocaaApplication.Div(100), CoocaaApplication.Div(100));
    }

    public HSVLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        mFocusViewRevision = new SlideFocusView.FocusViewRevision(CoocaaApplication.Div(100), CoocaaApplication.Div(100));
    }

    public void setAdapter(HSVAdapter adapter) {
        this.adapter = adapter;
        childViewList = new ArrayList<View>();
        for (int i = 0; i < adapter.getCount(); i++) {
            View view = adapter.getView(i, null, null);
            view.setPadding(4, 4, 4, 4);
            view.setOnClickListener(mOnClickListener);
            view.setOnKeyListener(mOnKeyListener);
            view.setTag(i);
            view.setId(i);
            this.setOrientation(HORIZONTAL);
            LinearLayout.LayoutParams leftParams = new LinearLayout.LayoutParams(CoocaaApplication.Div(240), CoocaaApplication.Div(330));
            if (i != 0)
                leftParams.leftMargin = CoocaaApplication.Div(30);

            this.addView(view, leftParams);
            childViewList.add(view);
            if (mFocusChangedEvent != null) {
                mFocusChangedEvent.registerView(view, mFocusViewRevision, mOnFocusChangeListener);//(OnFocusChangeListener)view);
            }
        }
    }

    public void setChildItemFocusByIndex(int index) {
        if(childViewList == null || index < 0 || index >= childViewList.size()) {
            return;
        }

        childViewList.get(index).requestFocus();
    }

    public void setmonFocusChange(OnFocusChangeListener listener) {
        mOnFocusChangeListener = listener;
    }

    public void setmOnClickListener(OnClickListener listener) {
        this.mOnClickListener = listener;
    }

    public void setmFocusChangedEvent(SlideFocusView.FocusChangedEvent focusChangedEvent) {
        this.mFocusChangedEvent = focusChangedEvent;
    }

    public void setmOnKeyListener(OnKeyListener listener) {
        this.mOnKeyListener = listener;
    }
}
